#include "hexutil.h"

QString HexUtil::convertByteArrayToString(const QByteArray &byteArray, EncodingMode mode)
{
    switch (mode) {
    case EncodingMode::ASCII:
        return QString::fromLatin1(byteArray);
    case EncodingMode::GB2312: {
        // 获取 GB2312 的文本编解码器
        QTextCodec *codec = QTextCodec::codecForName("GB2312");
        if (codec) {
            // 使用编解码器将 QByteArray 转换为 QString
            return codec->toUnicode(byteArray);
        }
    } break;
    default:
        break;
    }
    // 如果模式不匹配或者 GB2312 编解码器不可用，返回空字符串
    return QString();
}

QByteArray HexUtil::hexStringToByteArray(const QString &hexString, const QString &separator)
{
    QByteArray byteArray;
    QString processedHex = hexString;
    if (!separator.isEmpty()) {
        processedHex = processedHex.replace(separator, "");
    }
    if (processedHex.length() % 2 != 0) {
        return byteArray;
    }
    for (int i = 0; i < processedHex.length(); i += 2) {
        QString hexByte = processedHex.mid(i, 2);
        bool ok;
        char byte = static_cast<char>(hexByte.toInt(&ok, 16));
        if (ok) {
            byteArray.append(byte);
        }
    }
    return byteArray;
}

bool HexUtil::isValidHexString(const QString &hexString, const QString &separator)
{
    QString processedHex = hexString;
    if (!separator.isEmpty()) {
        processedHex = processedHex.replace(separator, "");
    }
    if (processedHex.length() % 2 != 0) {
        return false;
    }
    for (int i = 0; i < processedHex.length(); i += 2) {
        QString hexByte = processedHex.mid(i, 2);
        bool ok;
        hexByte.toInt(&ok, 16);
        if (!ok) {
            return false;
        }
    }
    return true;
}

QString HexUtil::qStringToGb2312HexWithSpaces(const QString &str)
{
    QTextCodec *codec = QTextCodec::codecForName("GB2312");
    if (!codec) {
        return QString();
    }
    QByteArray byteArray = codec->fromUnicode(str);

    QString hexString;
    hexString.reserve(byteArray.size() * 3);
    for (int i = 0; i < byteArray.size(); ++i) {
        hexString.append(
            QString("%1").arg(static_cast<uchar>(byteArray[i]), 2, 16, QChar('0')).toUpper());
        if (i != byteArray.size() - 1) {
            hexString.append(" ");
        }
    }

    return hexString;
}

QString HexUtil::qStringToAsciiHexWithSpaces(const QString &str,
                                             bool replaceNonAscii,
                                             char replacement)
{
    QByteArray byteArray;
    byteArray.reserve(str.length());

    for (int i = 0; i < str.length(); ++i) {
        ushort unicodeChar = str[i].unicode();

        if (unicodeChar >= 0x20 && unicodeChar <= 0x7E) {
            byteArray.append(static_cast<char>(unicodeChar));
        } else {
            if (replaceNonAscii) {
                byteArray.append(replacement);
            }
        }
    }

    QString hexString;
    hexString.reserve(byteArray.size() * 3);
    for (int i = 0; i < byteArray.size(); ++i) {
        hexString.append(
            QString("%1").arg(static_cast<uchar>(byteArray[i]), 2, 16, QChar('0')).toUpper());
        if (i != byteArray.size() - 1) {
            hexString.append(" ");
        }
    }

    return hexString;
}
